package Weapons;

import game.Config.Config;

import org.newdawn.slick.geom.Vector2f;

/**
 * This class represents a weapon in the game. The weapon can be fired,
 * and if the weapon is ready it will spawn a bullet.
 * How the weapon spawns bullets and the bullets spawned are 
 * determined by the methods spawnBullet and callFactory.
 * 
 * @author Sebastian Isheden and Victor Beer
 *
 */
public abstract class Weapon {
	protected float fireInterval; //perseconds
	protected float damage;
	protected float bulletSpeed;
	private boolean isReady;
	private float currentFireTime;
	/**
	 * Creates a new Weapon with given parameters.
	 * @param fireRate	The amount of times this weapon can fire per second.
	 * @param damage  The damage per shot of this weapon.
	 * @param bulletSpeed  The speed the fired bullets will traverse the screen.
	 */
	public Weapon(float fireRate,float damage,float bulletSpeed) {
		this.fireInterval = (1/fireRate);
		this.currentFireTime = this.fireInterval;
		this.damage = damage;
		this.bulletSpeed = bulletSpeed;
	}
	/**
	 * @return	A copy of the Weapon.
	 */
	public abstract Weapon copy();
	/**
	 * Updates the weapon. If enough time has passed,
	 * the weapon will become ready to fire. 
	 * The amount of time between shots is determined by 1/fireRate,
	 * where fireRate is the float given in the constructor.
	 * @param delta Time in milliseconds since last update.
	 */
	public void update(int delta) {
		this.currentFireTime -= (float)delta/Config.MILLISECONDS;
		if(this.currentFireTime <= 0) {
			this.isReady = true;
		}
	}
	

	/**
	 * Calling this method will make the weapon fire, if it is ready to fire. 
	 * @param position The position the bullet will spawn at
	 * @param target The direction the bullet will move in after it has spawned. 
	 */
	public void fire(Vector2f position, Vector2f target) {
		if(this.isReady) {
			this.spawnBullet(position, target);
			this.isReady = false;
			this.currentFireTime = this.fireInterval;
		}
	} 
	protected abstract void spawnBullet(Vector2f position, Vector2f direction);
	protected abstract void callFactory(Vector2f position, Vector2f direction);

	
}

